package com.mxx.pmxt.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mxx.pmxt.dao.PUsersModelMapper;
import com.mxx.pmxt.model.PUsersModel;
import com.mxx.pmxt.service.PUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 马潇霄 on 2017/3/23.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PUserServiceImpl implements PUserService
{
    @Resource
    PUsersModelMapper pUsersModelMapper;

    public JSONObject getAllUser()
    {
        JSONObject jsonObject = new JSONObject();
        List<PUsersModel> pUsersModels = pUsersModelMapper.getAllUser();
        jsonObject.put("userList",pUsersModels);
        return jsonObject;
    }

    public int insertUser(String uname, String upassword, String urealname, Integer usex, String utel, String uaddress)
    {
        PUsersModel pUsersModel = new PUsersModel();
        pUsersModel.setUname(uname);
        pUsersModel.setUpassword(upassword);
        pUsersModel.setUrealname(urealname);
        pUsersModel.setUtype(0);//0为普通用户，1为管理员（管理员只有一位），-1为禁用账户
        pUsersModel.setUsex(usex);
        pUsersModel.setUtel(utel);
        pUsersModel.setUaddress(uaddress);
        BigDecimal account = new BigDecimal(0);
        pUsersModel.setUaccount(account);//默认初始账户为0元

        return pUsersModelMapper.insertSelective(pUsersModel);
    }

    public int checkUName(String uname)
    {
        List<PUsersModel> pUsersModels = pUsersModelMapper.getUserByName(uname);
        if (pUsersModels.isEmpty()){
            return 0;
        }else{
            return 1;
        }

    }

    public int checkLogin(String uname, String password)
    {
        List<PUsersModel> pUsersModels = pUsersModelMapper.getUserByName(uname);
        if (pUsersModels.isEmpty()){
            return 0;//用户名不存在
        }
        String relpassword = pUsersModels.get(0).getUpassword();
        int utype = pUsersModels.get(0).getUtype();
        if (password.equals(relpassword)){
            if (utype==1){
                return 3;//管理员
            }
            return 1;//密码正确
        }else {
            return 2;//密码不正确
        }
    }

    public Map<String, String> showUserinfo(String uname)
    {
        PUsersModel pUsersModel = pUsersModelMapper.getUserByName(uname).get(0);
        String name = pUsersModel.getUname();
        String tel =pUsersModel.getUtel();
        String relname = pUsersModel.getUrealname();
        String address = pUsersModel.getUaddress();
        String address1 = address.split(" ")[0];
        String adrress2 = address.split(" ")[1];
        String sex = pUsersModel.getUsex().toString();

        Map<String,String> map = new HashMap<String, String>();
        map.put("name",name);
        map.put("tel",tel);
        map.put("relname",relname);
        map.put("address1",address1);
        map.put("address2",adrress2);
        map.put("sex",sex);
        return map;
    }

    public int modifyUser(String afteruname, String uname, String upassword, String urealname, Integer usex, String utel, String uaddress)
    {
        PUsersModel pUsersModel = pUsersModelMapper.getUserByName(uname).get(0);
        if (!afteruname.equals(uname)){
            List<PUsersModel> aftermodel = pUsersModelMapper.getUserByName(afteruname);
            if (!aftermodel.isEmpty()){
                return 0;//用户名已存在
            }
            pUsersModel.setUname(afteruname);
        }
        pUsersModel.setUaddress(uaddress);
        pUsersModel.setUpassword(upassword);
        pUsersModel.setUsex(usex);
        pUsersModel.setUtel(utel);
        pUsersModel.setUrealname(urealname);
        pUsersModelMapper.updateByPrimaryKeySelective(pUsersModel);
        return 1;//更新成功
    }

    public String getUserAccount(String uname)
    {
        List<PUsersModel> pUsersModels = pUsersModelMapper.getUserByName(uname);
        if (!pUsersModels.isEmpty()){
            return pUsersModels.get(0).getUaccount().toString();
        }
        return "";
    }


    public BigDecimal saveMoney(BigDecimal money,String name)
    {
        List<PUsersModel> pUsersModels = pUsersModelMapper.getUserByName(name);
        if (pUsersModels.isEmpty()){
            return money;
        }
        PUsersModel pUsersModel = pUsersModels.get(0);
        BigDecimal beforeAccount = pUsersModel.getUaccount();
        pUsersModel.setUaccount(beforeAccount.add(money));
        pUsersModelMapper.updateByPrimaryKeySelective(pUsersModel);
        return pUsersModelMapper.getUserByName(name).get(0).getUaccount();
    }

}
